package com.haoshuang.zhouzhoubang.ui.activity

import android.content.Context
import android.os.Bundle
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ActivityBalanceDetailsBinding
import com.haoshuang.zhouzhoubang.model.bean.BalanceDetailGroupListBean
import com.haoshuang.zhouzhoubang.ui.adapter.BalanceCate
import com.haoshuang.zhouzhoubang.ui.adapter.BalanceCateAdapter
import com.haoshuang.zhouzhoubang.ui.adapter.BalanceDetailGroupAdapter
import com.haoshuang.zhouzhoubang.ui.vm.BalanceDetailsViewModel
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.setEmptyData
import com.mufeng.libs.core.setOnItemClick
import com.mufeng.libs.core.setupData
import com.mufeng.libs.core.setupEmptyData
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.vertical


/**
 *  余额明细
 */
class BalanceDetailsActivity() :
    BaseVMActivity<BalanceDetailsViewModel, ActivityBalanceDetailsBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<BalanceDetailsActivity>()
        }
    }

    // 1订单收入 2-提现收入 3-邀请奖励 4-订单奖励 5-赔偿诚意金 6-违约金 7-保证金
    private val cateList = arrayListOf(
        BalanceCate(0, "全部", true),
        BalanceCate(1, "订单收入"),
        BalanceCate(3, "邀请奖励"),
        BalanceCate(4, "订单奖励"),
        BalanceCate(8, "推广奖励"),
        BalanceCate(5, "赔偿诚意金"),
        BalanceCate(6, "违约金"),
        BalanceCate(7, "保证金"),
        BalanceCate(9, "核销订单"),
    )

    private var type = 0 // 默认全部
    private val cateAdapter = BalanceCateAdapter(cateList)

    private val data = arrayListOf<BalanceDetailGroupListBean.BalanceDetailGroupBean>()
    /*
    reward_money：订单奖励
team_service_work：服务佣金
upload_video_award：上传视频奖励
team_frozen_money：违约扣除金额
violation：违约扣除金额
skill_money：保证金缴纳
pay_product：商城购物
team_refund_deposit：保证金退款
evaluate_deduct_money：差评扣除金额
system_add：系统增加
spread_service：分销佣金
pay_service：余额支付购买服务
other_cost：支付额外费用
extract：提现
skill_frozen_money：违约扣除保证金
     */
    private val typeList = listOf(
        "reward_money",// 订单奖励
        "reward_money",// 订单奖励
        "team_service_work",// 服务佣金
        "team_service_work_hexiao",// 核销
        "upload_video_award",//上传视频奖励
        "team_frozen_money",//违约扣除金额
        "violation",//违约扣除金额
        "evaluate_deduct_money",//差评扣除金额
        "other_cost",//支付额外费用
        "skill_frozen_money",//违约扣除保证金
    )
    private val adapter = BalanceDetailGroupAdapter(data){item, position ->
        if (item == null) return@BalanceDetailGroupAdapter
//        if (item.order_type == 2) return@BalanceDetailGroupAdapter
        if (item.type in typeList && item.order_id?.isNotEmpty() == true && item.order_id != "0"){
            if (item.type=="team_service_work_hexiao"){
                startPage<MxDetailActivity>("id" to item.id.toString(),"isHexiao" to true)
            }else{
                startPage<MxDetailActivity>("id" to item.id.toString(),"isHexiao" to false)
            }

        }
    }

    override fun initView(savedInstanceState: Bundle?) {

        binding.titleBar.title = "余额明细"
        binding.titleBar.leftImageView().click { finish() }

        binding.rvCate.vertical(4).adapter = cateAdapter
        cateAdapter.setOnItemClick { view, position ,item->
            val item = cateAdapter.getItem(position)
            type = item?.id ?:0
            cateList.map { it.isChoose = it.id == item?.id }
            cateAdapter.notifyDataSetChanged()
            binding.refreshLayout.autoRefresh()
        }

        binding.recyclerView.vertical()
            .adapter = adapter
        adapter.setEmptyData(R.mipmap.icon_empty_money, "暂无收支数据")

        binding.refreshLayout.setOnRefreshListener {
            isShowing = false
            p = 1
            vm.masterBillGroupByDayList(type, p)
        }

        binding.refreshLayout.setOnLoadMoreListener {
            isShowing = false
            p ++
            vm.masterBillGroupByDayList(type, p)
        }

    }

    override fun initData() {

        p = 1
        vm.masterBillGroupByDayList(type, p)

    }

    override fun startObserve() {
        vm.masterBillGroupByDayListData.observeState(this){
            onLoading {
                showLoading()
            }
            onError {code, msg ->
                dismissLoading()
                toast(msg)
                binding.refreshLayout.setupEmptyData(data, adapter, p)
            }
            onSuccess {
                dismissLoading()
                binding.refreshLayout.setupData(it?.list, data, adapter, p)
            }
        }
    }

}